<template>
  <el-form ref="basicInfoFormRef" :model="formModel" :rules="rules" label-width="150px">
    <el-row>
      <el-col :span="12">
        <el-form-item label="表名称" prop="tableName">
          <el-input placeholder="请输入仓库名称" v-model="formModel.tableName" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="表描述" prop="tableComment">
          <el-input placeholder="请输入" v-model="formModel.tableComment" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="实体类名称" prop="className">
          <el-input placeholder="请输入" v-model="formModel.className" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="作者" prop="functionAuthor">
          <el-input placeholder="请输入" v-model="formModel.functionAuthor" />
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" :rows="3" v-model="formModel.remark"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script setup lang="ts">
import { ref, defineProps, defineExpose, computed, watch } from 'vue'
import type { FormInstance } from 'element-plus'

// 定义信息对象接口
interface Info {
  tableName?: string
  tableComment?: string
  className?: string
  functionAuthor?: string
  remark?: string
  [key: string]: any
}

// 定义属性
interface Props {
  info: Info
}

const props = defineProps<Props>()

// 表单引用
const basicInfoFormRef = ref<FormInstance>()

// 创建本地响应式数据
const formModel = ref({ ...props.info })

// 监听props.info变化更新本地数据
watch(
  () => props.info,
  (newVal) => {
    formModel.value = { ...newVal }
  },
  { deep: true },
)

// 规则
const rules = {
  tableName: [{ required: true, message: '请输入表名称', trigger: 'blur' }],
  tableComment: [{ required: true, message: '请输入表描述', trigger: 'blur' }],
  className: [{ required: true, message: '请输入实体类名称', trigger: 'blur' }],
  functionAuthor: [{ required: true, message: '请输入作者', trigger: 'blur' }],
}

// 暴露组件实例属性
defineExpose({
  basicInfoFormRef,
  formModel,
})
</script>
